package com.badibadi.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalBitmap;

import com.badibadi.abcpinying.SortModel;
import com.badibadi.activity.AddFriendValidationActivity;
import com.badibadi.activity.MyDataActivity;
import com.badibadi.activity.PinyinSeeRecordParticipantsActivity;
import com.badibadi.activity.SeeOtherPeopleSpaceActivity;
import com.badibadi.infos.UserListModel;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class XListView1Adapter_3 extends BaseAdapter {
	private Context context;
	private List<UserListModel> userListModels;
	private DisplayImageOptions options;

	public XListView1Adapter_3(Context context,
			List<UserListModel> userListModels) {
		this.context = context;
		this.userListModels = userListModels;
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.noimage)
				.showImageOnFail(R.drawable.noimage).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5)).build();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return userListModels.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return userListModels.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = LayoutInflater.from(context);
		convertView = inflater.inflate(
				R.layout.fragment_indexclub2_import_layout, null);
		ImageView user_list_item_img = (ImageView) convertView
				.findViewById(R.id.user_list_item_img);
		TextView user_list_item_name = (TextView) convertView
				.findViewById(R.id.user_list_item_name);
		TextView user_list_item_from = (TextView) convertView
				.findViewById(R.id.user_list_item_from);
		Button user_list_item_add = (Button) convertView
				.findViewById(R.id.user_list_item_add);
		user_list_item_name.setText(userListModels.get(position).getNickName());
		user_list_item_from.setText(userListModels.get(position).getAreaName());
		if (userListModels.get(position).getAdd().equals("0")) {
			user_list_item_add.setText(R.string.AddFriend);
			user_list_item_add.setTextColor(0xff3eb251);
			user_list_item_add.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(context,
							AddFriendValidationActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.putExtra("id", userListModels.get(position).getId());
					context.startActivity(intent);
				}
			});
		} else if (userListModels.get(position).getAdd().equals("1")) {
			user_list_item_add.setText(R.string.Friend);
			user_list_item_add.setTextColor(R.color.gray);
		}
		ImageLoader.getInstance().displayImage(
				Constants.BadiDownImgUrl
						+ userListModels.get(position).getHead()+Constants.appPhoto4img,
				user_list_item_img, options);
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(userListModels.get(position).getId()!=null)
				{
					if(!Utils.getUid(context).equals(""+userListModels.get(position).getId()))
					{
						Intent intent = new Intent(context,
								SeeOtherPeopleSpaceActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.putExtra("friend_id", userListModels.get(position)
								.getId());
						context.startActivity(intent);
					}else
					{
						Intent intent = new Intent(context,
								MyDataActivity.class);
//						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//						intent.putExtra("friend_id", userListModels.get(position)
//								.getId());
						context.startActivity(intent);
					}
				
				}
			
			}
		});
		return convertView;
	}
}
